<?php
/**
 * Hesperos library for Zend Framework
 *
 * This script is protected by copyright. It's use, copying, modification
 * and distribution without written consent of the author is prohibited.
 *
 * @category    Hesperos
 * @package     Bootstrap
 * @author      Krzysztof Kardasz <krzysztof.kardasz@gmail.com>
 * @copyright   Copyright (c) 2011 Krzysztof Kardasz
 * @license     http://www.gnu.org/licenses/lgpl-3.0.txt  GNU Lesser General Public
 * @version     1.0 $Id$
 * @link        http://code.google.com/p/hesperos/
 */

/**
 * Uruchomienie modułu
 *
 * @category    Hesperos
 * @package     Bootstrap
 * @author      Krzysztof Kardasz <krzysztof.kardasz@gmail.com>
 * @copyright   Copyright (c) 2011 Krzysztof Kardasz
 * @version     1.0 $Id$
 */
class Hesperos_Bootstrap_Module_Default extends Zend_Application_Module_Bootstrap
{
    /**
     * Ścieżka modułu
     *
     * @var string
     */
    private $_moduleBasePath = null;

    /**
     * Ścieżka bazowa modułu
     *
     * @return string
     */
    protected function getBasePath ()
    {
        if($this->_moduleBasePath === null) {
            $reflection = new ReflectionClass($this);
            $this->_moduleBasePath = dirname($reflection->getFileName());
        }
        return $this->_moduleBasePath;
    }

    /**
     * Rejestracja pomocnika akcji
     *
     * @return void
     */
    protected function _initHelperBroker ()
    {
        $dir = implode(DIRECTORY_SEPARATOR, array($this->getBasePath(), 'plugins','Action','Helpers'));
        Zend_Controller_Action_HelperBroker::addPath($dir, 'Helper');
    }

    /**
     * Rejestracja tłumaczeń
     *
     * @return void
     */
    protected function _initRegisterI18n ()
    {
        $moduleName = $this->getModuleName();
        $cache      = Hesperos_Cache_Manager::getI18n();

        $dir = implode(DIRECTORY_SEPARATOR, array($this->getBasePath(), 'i18n'));
        if(is_dir($dir)) {
            if(!$cache || ($files = $cache->load($moduleName)) === false) {
                $files    = array();
                $iterator = new DirectoryIterator($dir);
                foreach ($iterator as $fileinfo) {
                    if ($fileinfo->isFile()) {
                        $name = $fileinfo->getFilename();
                        if(preg_match('/[a-z]{2}\.mo/', $name)) {
                            $files[substr($fileinfo, 0, 2)] = $fileinfo->getRealPath();
                        }
                    }
                }
                if($cache) {
                    $cache->save($files, $moduleName);
                }
            }
            foreach ($files as $language => $file) {
                Hesperos_Translate_Manager::addTranslation($file, $language);
            }
        }
    }

    /**
     * Rejestracja kontroli uprawnień
     *
     * @return void
     */
    protected function _initRegisterAcl ()
    {
        $moduleName = $this->getModuleName();
        $cache      = Hesperos_Cache_Manager::getAcl();
        $moduleDir  = $this->getBasePath();
        $file       = implode(DIRECTORY_SEPARATOR, array($moduleDir, 'config', 'acl.xml'));

        if(is_file($file)) {
            if(!$cache || ($acl = $cache->load($moduleName)) === false) {
                $acl = new Zend_Acl ();

                Hesperos_Acl_Role::register($acl);

                $reader = new Hesperos_Util_Reader_Acl (file_get_contents($file));

                $resources = $reader->getResources();
                if(!empty($resources)) {
                    foreach($resources as $data) {
                        $acl->addResource($data['resource'], (isset($data['parent'])) ? $data['parent'] : null);
                    }
                }

                $allowed = $reader->getAllow();
                if(!empty($allowed)) {
                    foreach($allowed as $data) {
                        foreach($data['resources'] as $data2) {
                            $acl->allow($data['role'], $data2['resource'], (!empty($data2['privileges'])) ? $data2['privileges'] : null);

                        }
                    }
                }

                $denied = $reader->getDeny();
                if(!empty($denied)) {
                    foreach($denied as $data) {
                        foreach($data['resources'] as $data2) {
                            $acl->deny($data['role'], $data2['resource'], (!empty($data2['privileges'])) ? $data2['privileges'] : null);
                        }
                    }
                }

                if($cache) {
                    $cache->save($acl, $moduleName);
                }
            }
            Hesperos_Acl_Manager::set($moduleName, $acl);
        }
    }
}